import { bedList } from '@/services/deptMsg';
import { patientAdd, patientUpdate } from '@/services/patientMsg';
import {
  ProForm,
  ProFormDigit,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, Col, Row, Space, message } from 'antd';
import { useState } from 'react';

import './index.less';

const AddEditPatient = (props: any) => {
  const { ecgModuleItem = {} } = props;
  const [modItem, setModItem] = useState(ecgModuleItem?.patient || null);
  const [form] = ProForm.useForm();
  const savePatient = async () => {
    const values = await form.getFieldsValue();
    const sendData = {
      ...values,
      sex: values.sex ? Number(values.sex) : '',
      blood: values.blood ? Number(values.blood) : '',
    };
    console.log('sendData', sendData);
    let res = null;
    if (modItem?.id) {
      //编辑
      res = await patientUpdate({
        ...sendData,
        id: modItem?.id,
      });
    } else {
      //添加
      res = await patientAdd(sendData);
    }
    if (res?.code === 200) {
      message.success('操作成功');
      setModItem(res?.data);
    }
    return true;
  };
  return (
    <div className="addEditPatient-wrapper">
      <div className="header-wrapper">
        <div className="title-wrapper">患者管理</div>
        <Space className="option-btn-list" size={10}>
          <Button className="base-button" type="primary" onClick={savePatient}>
            修改
          </Button>
        </Space>
      </div>
      <ProForm
        submitter={false}
        className="addEditPatient-content"
        form={form}
        initialValues={{
          ...modItem,
          sex: modItem?.sex ? String(modItem.sex) : undefined,
          blood: modItem?.blood ? String(modItem.blood) : undefined,
        }}
      >
        <Row gutter={20}>
          <Col span={12}>
            <ProFormText
              name="name"
              label="患者姓名"
              fieldProps={{
                maxLength: 100,
                showCount: true,
              }}
            />
          </Col>
          <Col span={12}>
            <ProFormSelect
              name="sex"
              label="患者性别"
              placeholder="请选择患者性别"
              options={[
                {
                  label: '男',
                  value: '1',
                },
                {
                  label: '女',
                  value: '2',
                },
              ]}
            />
          </Col>
        </Row>
        <Row gutter={20}>
          <Col span={7}>
            <ProFormSelect
              name="blood"
              label="患者血型"
              placeholder="请选择患者血型"
              options={[
                {
                  label: 'A型',
                  value: '1',
                },
                {
                  label: 'B型',
                  value: '2',
                },
                {
                  label: 'AB型',
                  value: '3',
                },
                {
                  label: 'O型',
                  value: '4',
                },
                {
                  label: 'RH型',
                  value: '5',
                },
              ]}
            />
          </Col>
          <Col span={10}>
            <ProFormText
              name="phone"
              label="患者手机号"
              fieldProps={{
                maxLength: 11,
                showCount: true,
              }}
              placeholder="请输入患者手机号"
            />
          </Col>
          <Col span={7}>
            <ProFormDigit
              name="age"
              label="患者年龄"
              min={1}
              max={100}
              fieldProps={{ precision: 0 }}
            />
          </Col>
        </Row>
        <Row gutter={20}>
          <Col span={24}>
            <ProFormSelect
              name="bedId"
              label="关联病床"
              placeholder="请选择关联病床"
              allowClear
              request={async () => {
                const res = await bedList({
                  pageSize: 1000,
                  pageNum: 1,
                });
                if (res?.code === 200) {
                  return res.rows.map((its: any) => {
                    return {
                      label: its.code,
                      value: its.id,
                    };
                  });
                } else {
                  return [];
                }
              }}
            />
          </Col>
        </Row>
      </ProForm>
    </div>
  );
};
export default AddEditPatient;
